package com.rc.saas.tenant.service.banner;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.constant.TenantBannerConstant;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.mapper.banner.TenantBannerMapper;
import com.rc.saas.tenant.model.banner.TenantBanner;
import com.rc.saas.tenant.model.banner.TenantBannerExample;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.model.tenant.TenantExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author frog
 * @date 2019/11/22
 */
@Service
public class TenantBannerServiceImpl implements TenantBannerService {

    @Autowired
    private TenantBannerMapper tenantBannerMapper;

    @Autowired
    private UpYunConfig upYunConfig;

    @Override
    public TenantBannerMapper getMapper() {
        return tenantBannerMapper;
    }

    @Override
    public PageInfo<TenantBanner> listPageBanner(BaseSearch baseSearch, String bannerType) {
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        TenantBannerExample example = new TenantBannerExample();

        if (StringUtil.isBlank(bannerType)) {
            example.createCriteria().andTenantCodeEqualTo(baseSearch.getTenantCode());
            example.setOrderByClause("created_time DESC");
        } else {
            example.createCriteria().andTenantCodeEqualTo(baseSearch.getTenantCode()).andBannerTypeEqualTo(bannerType);
            example.setOrderByClause("created_time DESC");
        }
        List<TenantBanner> tenantBanners = tenantBannerMapper.selectByExample(example);
//        for (TenantBanner tenantBanner : tenantBanners) {
//            tenantBanner.setWechatIndexImg(upYunConfig.getDownloadRoot() + tenantBanner.getWechatIndexImg());
//            tenantBanner.setAppFlashAdImg(upYunConfig.getDownloadRoot() + tenantBanner.getAppFlashAdImg());
//            tenantBanner.setAppFlashImg(upYunConfig.getDownloadRoot() + tenantBanner.getAppFlashImg());
//            tenantBanner.setAppIndexImg(upYunConfig.getDownloadRoot() + tenantBanner.getAppIndexImg());
//            tenantBanner.setAppStartImg(upYunConfig.getDownloadRoot() + tenantBanner.getAppStartImg());
//            tenantBanner.setMiniIndexImg(upYunConfig.getDownloadRoot()+tenantBanner.getMiniIndexImg());
//        }
        return new PageInfo<TenantBanner>(tenantBanners);
    }

    @Override
    public TenantBanner selectBannerByTenantCode(String tenantCode) {
        TenantBannerExample example = new TenantBannerExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andTenantLogoImgIsNotNull();
        List<TenantBanner> tenantBanners = tenantBannerMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantBanners)) {
            return tenantBanners.get(0);
        }
        return null;
    }

}
